// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const savedvoiceCollection = db.collection('savedvoice')
const userCollection = db.collection('user')

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let { voiceContent, coverImg } = event
  let message = ''
  let voiceId = ''
  // let userInfo = {}
  // await userCollection.where({
  //     _openid: wxContext.OPENID
  //   }).get().then(res => {
  //     userInfo = res.data[0]
  //   }).catch(err => {

  //   })

  await savedvoiceCollection.add({
    data: {
      voiceContent,
      coverImg,
      // userInfo,
      _openid: wxContext.OPENID,
      createDate: new Date().getTime(),
      modifyDate: new Date().getTime(),
      submitDate: new Date().getTime(),
      likeCount: 0,
      commentCount: 0
    }
  }).then(res => {
    voiceId = res._id
    message = '保存成功'
  }).catch(err => {
    message = '保存失败'
  })
  return {
    message,
    voiceId
  }

}